<html>
<head><meta charset="utf-8"><title>cast-fn-ptr-unsafe tests · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html">cast-fn-ptr-unsafe tests</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="147758530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147758530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147758530">(Nov 15 2018 at 17:32)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@Oli</span> I don' get <a href="https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe.rs" target="_blank" title="https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe.rs">https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe.rs</a> and <a href="https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe2.rs" target="_blank" title="https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe2.rs">https://github.com/solson/miri/blob/master/tests/compile-fail/cast_fn_ptr_unsafe2.rs</a></p>



<a name="147758534"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147758534" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147758534">(Nov 15 2018 at 17:32)</a>:</h4>
<p>the comment doesn't even match what the test does</p>



<a name="147758544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147758544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147758544">(Nov 15 2018 at 17:32)</a>:</h4>
<p>rustc <em>does</em> allow casting <code>fn()</code> to <code>unsafe fn()</code></p>



<a name="147758549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147758549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147758549">(Nov 15 2018 at 17:32)</a>:</h4>
<p>and indeed why wouldn't it?</p>



<a name="147759497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147759497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147759497">(Nov 15 2018 at 17:49)</a>:</h4>
<p>my inclination is to just remove these two tests. we got tons of tests for what happens when you transmute a fn ptr and call a fn with the wrong signature. I do not know what these two add.</p>



<a name="147759588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147759588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147759588">(Nov 15 2018 at 17:50)</a>:</h4>
<p>Seems you added them with <a href="https://github.com/solson/miri/commit/00eb198a82376eeb608c32e6d4252743f6dcfc87" target="_blank" title="https://github.com/solson/miri/commit/00eb198a82376eeb608c32e6d4252743f6dcfc87">https://github.com/solson/miri/commit/00eb198a82376eeb608c32e6d4252743f6dcfc87</a> though sh probably there was a reason</p>



<a name="147800596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147800596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147800596">(Nov 16 2018 at 07:12)</a>:</h4>
<p>we want to prevent calling an <code>unsafe fn</code> via a <code>fn</code> pointer</p>



<a name="147800618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147800618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147800618">(Nov 16 2018 at 07:13)</a>:</h4>
<p>feel free to remove them, we do need to trust the type system</p>



<a name="147801880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147801880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147801880">(Nov 16 2018 at 07:49)</a>:</h4>
<blockquote>
<p>we want to prevent calling an <code>unsafe fn</code> via a <code>fn</code> pointer</p>
</blockquote>
<p>Do we? I can transmute one to the other and then call it. And I do not see a good way -- nor a good reason -- to disallow this.</p>



<a name="147801886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147801886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147801886">(Nov 16 2018 at 07:49)</a>:</h4>
<p>Or do you mean "in safe code"? That is a type system matter, miri shouldn't (and in many cases can't) care.</p>



<a name="147801967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147801967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147801967">(Nov 16 2018 at 07:51)</a>:</h4>
<p>I agree fully with the two things you said and have come to the same conclusion. Back then I think I added this just to be very sure</p>



<a name="147802040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147802040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147802040">(Nov 16 2018 at 07:53)</a>:</h4>
<p>unsafe code may convert an unsafe fn pointer to a safe one. That might actually be totally safe if e.g. the arguments change, too. If the arg was <code>*const T</code> that was dereferenced inside the function and we converted the function pointer to something taking a <code>&amp;T</code>, then that seems like a valid operation</p>



<a name="147802167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147802167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147802167">(Nov 16 2018 at 07:56)</a>:</h4>
<p>ack</p>



<a name="147802168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe%20tests/near/147802168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/cast-fn-ptr-unsafe.20tests.html#147802168">(Nov 16 2018 at 07:56)</a>:</h4>
<p>PR submitted</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>